package com.eightywork.life.activity;

import java.util.ArrayList;
import java.util.List;

import cn.jpush.android.api.JPushInterface;

import com.eightywork.life.R;
import com.eightywork.life.activity.PmListActivity.PmListTask;
import com.eightywork.life.adapter.NoticeListAdapter;
import com.eightywork.life.model.Notice;
import com.eightywork.life.service.SpaceService;
import com.eightywork.life.util.MyHttpException;
import com.eightywork.life.widget.FooterView;
import com.eightywork.life.widget.MyListView;
import com.eightywork.life.widget.MyListView.OnRefreshListener;
import com.umeng.analytics.MobclickAgent;



import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.AbsListView.OnScrollListener;

public class NoticeListActivity extends BaseActivity implements OnClickListener {
	private ImageButton backButton;
	private MyListView listView;
	private LinearLayout ll_noMsg, ll_listView;
	private TextView headerTitle, footerText;
	private boolean isLoading, isRefresh = false;
	private AsyncTask<?, ?, ?> task;
	private NoticeListAdapter adapter;
	private List<Notice> list ;
	private int page = 1;
	private int flag = 0;
//	private ViewGroup footView, nodataView;
	private FooterView footView;
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_notice_list);
		initView();
		initData();
		getNoticeList();
	}

	@Override
	public void onDestroy() {
		if (task != null)
			task.cancel(true);
		super.onDestroy();
	}
	
	@Override
	public void onResume() {		
		super.onResume();
		JPushInterface.onResume(this);
	    MobclickAgent.onResume(this);
	}
	
	@Override
	public void onPause() {
	    super.onPause();
		JPushInterface.onPause(this);
	    MobclickAgent.onPause(this);
	}
	
	private void initView() {
		
		headerTitle = (TextView) findViewById(R.id.header_title);
		backButton = (ImageButton) findViewById(R.id.button_back);
		listView = (MyListView) findViewById(R.id.listView2);
//		footView = (ViewGroup) getLayoutInflater().inflate(R.layout.item_footer, null);
//		nodataView = (ViewGroup) getLayoutInflater().inflate(
//				R.layout.item_footer_nodata, null);
//		footerText = (TextView) nodataView.findViewById(R.id.textView1);
		footView = new FooterView(this);
		footView.hideAll();
		ll_noMsg = (LinearLayout) findViewById(R.id.noMsg);
		ll_listView = (LinearLayout) findViewById(R.id.ll_listView);
		backButton.setOnClickListener(this);
		
	}
	
	private void initData() {
		initHeader("消息列表");
		adapter = new NoticeListAdapter(context, list);
		listView.setAdapter(adapter);
		listView.setonRefreshListener(new OnRefreshListener() {		
			@Override
			public void onRefresh() {
				// TODO Auto-generated method stub
				page = (flag == 0)? 1: 0;
				isLoading = false;
				isRefresh = true;
				getNoticeList();
				
			}
		});
	}
	
	private void getNoticeList() {
		task = new NoticeList().execute();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if ( v == backButton) {
			finish();
		}
	}
	
	class NoticeList extends AsyncTask<Void, Void, String> {
		List<Notice> nlist;
		@Override
		protected void onPreExecute() {
			isLoading = true;
			if (flag == 0) {
				showProgressDialog("正在加载中...");
			} else {
				page++;
			}
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			try {
				nlist = SpaceService.notice(page + "", "");
			} catch (MyHttpException e) {
				// TODO Auto-generated catch block
				showMessage(e.getErrorMessage());
			}
			return null;
		}
		@Override
		protected void onPostExecute(String result) {
			isLoading = false;
			if (flag == 0) {
				hideProgressDialog();
			}
			if (isRefresh) {
				listView.onRefreshComplete();
			}
			if (nlist != null && nlist.size() > 0) {
				if (flag == 0) {
					adapter = new NoticeListAdapter(context, nlist);
					listView.addFooterView(footView.getFooter());
					listView.setAdapter(adapter);
					listView.setOnScrollListener(new ScrollListener());
					list = nlist;
					flag = 1;
				} else if (isRefresh) {
					isRefresh = false;
					adapter = new NoticeListAdapter(context, nlist);
					listView.setAdapter(adapter);
					listView.setOnScrollListener(new ScrollListener());
					list = nlist;
				} else {
					list.addAll(nlist);
					adapter.notifyDataSetChanged();
				} if (nlist.size() < 5) {
					footView.showNoData();
				}
			} else {
				if (nlist != null) {
					if (flag == 0) {
						ll_listView.setVisibility(View.GONE);
						ll_noMsg.setVisibility(View.VISIBLE);
					} else {
						footView.showNoData();
					}
				} else {
					footView.hideAll();
					showMessage("网络链接失败，请稍后重试！");
				}
			}
			super.onPostExecute(result);
		}
	
	}
	
	 private class ScrollListener implements OnScrollListener
		{
			private int listViewLastItem = 0;
			private boolean isOver = false;
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
			{
				if (adapter.getCount() % page > 0 || adapter.getCount() == 0)
				{
					footView.hideAll();
					isOver = true;
				}
				listViewLastItem = firstVisibleItem + visibleItemCount - 1; 
			}

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState)
			{    
				if (!isOver && !isLoading && listViewLastItem >= adapter.getCount()-5 
						&& scrollState == OnScrollListener.SCROLL_STATE_IDLE)
				{
					footView.showLoading();
					task = new NoticeList().execute();
				}

			}
		}

}
